/**
 * @file record.h
 * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
 */

#ifndef RECORD_H
#define RECORD_H

#include <stddef.h>
#include <stdint.h>
#include <cx_record.h>
#include "pthread.h"
#include "cx_utility.h"

#ifdef __cplusplus
extern "C" {
#endif

#define RECORD_INDEX_FRAME_HEAD 0xFCFC
#define RECORD_VIDEO_FRAME_MAX_SIZE (100*1024)
#define RECORD_AUDIO_FRAME_MAX_SIZE (200*16*2)
#define RECORD_VIDEO_SEGMENT_MAX_SIZE (50*1024*1024)
#define RECORD_VIDEO_RINGBUFFER_SIZE (1024*1024)
#define RECORD_AUDIO_RINGBUFFER_SIZE (100*1024)
typedef struct {
    char *root_path;
    cx_record_video_save_type_e save_type;
    pthread_mutex_t record_mutex;
    pthread_mutex_t record_index_mutex;
    FILE *fp_video;
    FILE *fp_audio;
    FILE *fp_record_index;
    unsigned short crc_index;
    unsigned short crc_video;
    unsigned short crc_audio;
    int task_running_flag;
    int record_running_flag;
    char record_raw_data_file_name[MAX_RECORD_NAME_LEN];
    int record_segment;
    int duration;
    time_t start_time;
    int video_size;
    int audio_size;
    cx_dev_ringbuf_t record_video_rb;
    cx_dev_ringbuf_t record_audio_rb;
    char *record_video_ringbuffer;
    char *record_audio_ringbuffer;
    int record_video_rb_peak;
    int record_audio_rb_peak;
} cx_record_priv_t;

typedef struct {
    uint8_t fps;
    uint8_t video_type;
    uint16_t reserve;
} cx_video_param_e;

typedef struct {
    uint8_t channel:4;
    uint8_t audio_type:4;
    uint8_t sample_bits;
    uint16_t sample_rate;
} cx_audio_param_e;

typedef struct {
    long long start_time;  //s
    unsigned int duration; //ms
    unsigned int file_size;
    cx_video_param_e video_param;
    cx_audio_param_e audio_param;
    unsigned int segment_num;
    int valid;
    uint8_t reserve[30];
    uint16_t crc; 
} cx_record_index_head;

typedef struct {
    long long start_time;  //s
    unsigned int duration; //ms
    unsigned int next_segment_pos;
} cx_record_index_segment_head;

typedef struct {
    uint16_t head;
    uint8_t media_type;
    uint8_t key_frame_flag;
    uint32_t offset;
    uint32_t len;
    uint32_t time_stamp;
} cx_record_index_frame;


/** @} */

#ifdef __cplusplus
}
#endif

#endif /* RECORD_H */

